<template>
	<view>
		<!-- 选项卡 -->
		<view>
			<uni-segmented-control :current="current" :values="items" @clickItem="onClickItem" styleType="text" activeColor="#FF6622"></uni-segmented-control>
		</view>
        <!-- 选项卡详情 -->
		<view class="content" v-if="swShow === true">
			<view v-show="current === 0">
				<orderdetail :orderSummaryArr = "orderSummaryArr" @refreshPage="refreshPag"></orderdetail>
			</view>
			<view v-show="current === 1">
				<orderdetail :orderSummaryArr = "orderSummaryArr" @finishOrder="finishOrder"></orderdetail>
			</view>
			<view v-show="current === 2">
				<orderdetail :orderSummaryArr = "orderSummaryArr"></orderdetail>
			</view>
		</view>
		<view class="tips" v-else="swShow === false">
			<view class="txt">暂无订单数据</view>
		</view>
	</view>
</template>

<script>
	// 引用订单简介模板
	import orderDetail from '../../components/order/orderDetail.vue'
	// 引用公共函数
	import util from '../../utils/util.js'
	export default {
		//组件注册
		components:{
			"orderdetail":orderDetail
		},
		data() {
			return {
				current:0,
				items:['待取货','配送中','已完成'],
				orderSummaryArr:[
				],
				orderStatus:{
					DELIVERED:0,  //待配送
					RECEIVED:1,   //已接单
					CANCELED:2,   //已取消
					GETED:3,      //已取货
					FINISHED:4    //已送达
				},
				swShow:false
			}
		},
		onLoad() {
			//加载 已接单状态的订单
			this.requestOrder(this.orderStatus.RECEIVED);
		},
		methods: {
			//-------------------------事件监听------------------------
			//本页面的事件
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex
					if (this.current === 1){ 
						//请求 已取货状态(配送中)的订单
						this.requestOrder(this.orderStatus.GETED);
					}
					else if(this.current === 2){
						//请求 已完成状态的订单
						this.requestOrder(this.orderStatus.FINISHED);
					}
					else{
						//请求 已接单状态的订单
						this.requestOrder(this.orderStatus.RECEIVED);
					}
				}
			},
			//子页面（模板页面）的事件
			refreshPag(){
				//重新加载页面
				//请求 已接单状态的订单
				console.log('已收到子组件的消息');
//				this.requestOrder(this.orderStatus.RECEIVED);
			},
			finishOrder(){
				//重新加载页面
				//请求 已取货状态(配送中)的订单
				console.log('已收到子组件的消息');
//				this.requestOrder(this.orderStatus.GETED);
			},
			//-------------------------业务代码--------------------------
			requestOrder:function(taskno){
				var that = this;
				var data = {
					usertoken:uni.getStorageSync('usertoken'),
					ordersta:taskno
				};
				console.log(data);
				util.post('/horseman/gettask',data,(res) => {
					console.log(res);
					that.orderSummaryArr = res;
					that.swShow = true;
					if (res.status === 0)
					{   
						if (that.orderSummaryArr.length > 0){
							uni.showToast({
							    title: '已是最新数据',
							    duration: 2000
							});
						}
						else{
							that.swShow = false;
						}
						return;
					}
					for (var i = 0;i < res.length;i ++)
					{
						that.orderSummaryArr[i].create_time = util.formatDate(res[i].create_time);
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.tips{
		width: 100%;
		height: 1032rpx;  //通过计算
		display: flex;
		justify-content: center;
		align-items: center;
	}
</style>
